import { toSuccess, useEntityManager } from "@sker/core"
import { useData } from "@sker/runtime";
import { AutoTaskScript, AutoTaskScriptNotFound } from "@sker/entities";
import { z } from 'zod'

export default async () => {
    const input = z.object({
        type: z.string(),
        id: z.string(),
    });
    const body = input.parse(useData());
    const id = parseInt(body.id)
    const detail = await useEntityManager(async m => {
        if (body.type === `1`) {
            return await m.findOne(AutoTaskScript, { where: { id: id } })
        } else {
            const data = await m.findOne(AutoTaskScriptNotFound, { where: { id: id } })
            const item = await m.findOne(AutoTaskScript, { where: { task_name: data.task_name, package_name: data.package_name } })
            return {
                ...data,
                name: item?.name,
                display_order: item?.display_order || 0,
                timeout: item?.timeout || 3000,
                title: item?.title || data.desc,
                code: item?.code || `console.log('start')`
            };
        }
    })
    return toSuccess(detail)
}